package xyz.xtt.user.service.impl;

import static xyz.xtt.common.asset.AssertUtils.isFalse;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xyz.xtt.user.api.enums.UserFundRecordChangeStatus;
import xyz.xtt.user.api.enums.UserFundRecordUserType;
import xyz.xtt.user.constants.ErrorMessage;
import xyz.xtt.user.entity.TblUserFundRecord;
import xyz.xtt.user.mapper.TblUserFundRecordMapper;
import xyz.xtt.user.service.ITblUserFundRecordService;

/**
 * <p>
 * 用户资金账户变动记录表 服务实现类
 * </p>
 *
 * @author dale
 * @since 2025-02-25
 */
@Service
public class TblUserFundRecordServiceImpl extends ServiceImpl<TblUserFundRecordMapper, TblUserFundRecord> implements ITblUserFundRecordService {

	@Override
	public List<TblUserFundRecord> getNeedSettleRecords(int maxCount, Long lastId) {
		isFalse(maxCount > 0 && maxCount <= 20, ErrorMessage.ARGUMENT_NOT_VALID, () -> log
				.error("getNeedSettleRecords failed. maxCount must be greater than 0 and less than 20."));
		LambdaQueryChainWrapper<TblUserFundRecord> query = new LambdaQueryChainWrapper<>(baseMapper);
		if (lastId != null && lastId > 0) {
			query.gt(TblUserFundRecord::getId, lastId);
		}
		return query
				.le(TblUserFundRecord::getSettlePlanTime, LocalDateTime.now())
				.eq(TblUserFundRecord::getChangeStatus, UserFundRecordChangeStatus.UN_SETTLE.getCode())
				.eq(TblUserFundRecord::getUserType, UserFundRecordUserType.SELLER.getCode())
				.last("limit " + maxCount)
				.list();
	}

}
